//
//  PersonView.m
//  Brand
//
//  Created by Faker on 5/23/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "PersonView.h"
#import "Header.h"

#define DefaultInfoLabelHeight     139
#define DefaultInfoBgHeight        190

@interface PersonView()

@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;

@property (weak, nonatomic) IBOutlet UIImageView *personBgView;

@property (weak, nonatomic) IBOutlet UIImageView *personImageView;

@property (weak, nonatomic) IBOutlet UIView *infoBgView;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;

@property (strong, nonatomic) NSMutableArray* picArray;
@property (strong, nonatomic) NSArray* imageArray;
@property (assign, nonatomic) int picIndex;

@end

@implementation PersonView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // Initialization code
    }
    return self;
}

//如果用IB创建，则需要而外调用setUp方法。
-(id) initWithCoder:(NSCoder *)aDecoder{
    self =[super initWithCoder:aDecoder];
    if (self) {
       //只会加载两个进去
        [[[NSBundle mainBundle] loadNibNamed:@"PersonCell" owner:self options:nil] lastObject];
    }
    return self;
}

-(void)setUpWithPersonObj:(IntroObj *)obj
{
    self.picIndex = 0;
    self.picArray = [[NSMutableArray alloc]init];
    [self addSubview:self.mainView];
   
    self.imageArray = obj.picArray;
    
    self.myScrollView.contentHeight = self.myScrollView.height+1;
    self.infoLabel.height = DefaultInfoLabelHeight;
    self.infoBgView.height = DefaultInfoBgHeight;
    
    float nowLabelHeight = [Common handleTheLabelHeight:self.infoLabel withText:obj.content withMaxHeight:5000];
    float addHeight = nowLabelHeight>DefaultInfoLabelHeight?nowLabelHeight-DefaultInfoLabelHeight : 0;
    self.infoBgView.height += addHeight;
   
    self.myScrollView.contentHeight += addHeight;
    if(!isiPhone5)
    {
        self.myScrollView.contentHeight += 100;
    }

    
    //在下载图片之前先把之前的动画关掉
    if([self.personImageView isAnimating])
    {
        [self.personImageView stopAnimating];
    }
    
    [self startDownLoadImage];
    
    [self startAnimation];
    
}

#pragma mark - 图片下载
-(void)startDownLoadImage
{
    
    //[kApp displayWaitViewWithAllMaskView:NO];
    if(self.picIndex<[self.imageArray count])
    {
        NSURL* url = [NSURL URLWithString:[self.imageArray objectAtIndex:self.picIndex]];

        [[SDWebImageManager sharedManager] downloadWithURL:url options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
            
            if(!error)
            {
                
                self.picIndex ++;
                [self.picArray addObject:image];
                [self startDownLoadImage];
                [self changeImage];
            }
        }];
    }
    
}

-(void)changeImage
{
    [kApp hideWaitView];
    
    if([self.picArray count] == [self.imageArray count])
    {
        self.personImageView.animationImages = self.picArray;
        self.personImageView.animationDuration = 3;
        self.personImageView.animationRepeatCount = CGFLOAT_MAX;
        [self.personImageView startAnimating];
    }
}


#pragma mark - 动画
-(void)startAnimation
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI];
    rotationAnimation.duration = 8;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = CGFLOAT_MAX;
    rotationAnimation.removedOnCompletion = NO;
    [self.personBgView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}


@end
